<?php

//广告控制器
class Ads extends Controller {

	var $viewpath;
	var $is_logined;
	var $prepage = 20;
	
	function Ads()
	{
		parent::Controller();
		$this->viewpath = 'admin/'.__CLASS__;
		$this->load->model('managers_model','mm');
		$this->load->model('ads_model');
		$this->load->model('site_model','sm');
		$this->load->model('classes_model','cm');
		$this->is_logined = $this->mm->is_logined();
	}
	
	//默认首页
	function index()
	{
		if(!$this->is_logined) {
			redirect($this->config->item('admin_path')."/main/login");
		} 
		$manager = $this->mm->get_account();	
		//获取列表
		$items = $this->ads_model->list_placeitem(u(4), $this->prepage);
		//分页数据处理
		$this->load->library('pagination');
		$config['base_url'] = site_url($this->config->item('admin_path')."/ads/index");
		$config['total_rows'] = $this->ads_model->count_placeitems();
		$config['per_page'] = $this->prepage;
		$config['uri_segment'] = 4;
		$config['num_links'] = 1;
		$config['first_link'] = '首页';
		$config['last_link'] = '末页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		$this->pagination->initialize($config);
		$pagestring = $this->pagination->create_links();
		
		$viewdata['viewpath'] = $this->viewpath;
		$viewdata['manager'] = $manager;
		$viewdata['items'] = $items;
		$viewdata['pagestring'] = $pagestring;
		$this->load->view($this->viewpath.'/index', $viewdata);
	}
	
	//广告列表
	function lists()
	{
		if(!$this->is_logined) {
			redirect($this->config->item('admin_path')."/main/login");
		} 
		$manager = $this->mm->get_account();	
		//处理参数
		$placeid = u(4);
		if(!$placeid) $placeid = 0;
		
		//获取列表
		$items = $this->ads_model->list_item($placeid, u(5), $this->prepage);
		//分页数据处理
		$this->load->library('pagination');
		$config['base_url'] = site_url($this->config->item('admin_path')."/ads/lists/".$placeid);
		$config['total_rows'] = $this->ads_model->count_items($placeid);
		$config['per_page'] = $this->prepage;
		$config['uri_segment'] = 5;
		$config['num_links'] = 1;
		$config['first_link'] = '首页';
		$config['last_link'] = '末页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		$this->pagination->initialize($config);
		$pagestring = $this->pagination->create_links();
		
		$viewdata['viewpath'] = $this->viewpath;
		$viewdata['manager'] = $manager;
		$viewdata['items'] = $items;
		$viewdata['pagestring'] = $pagestring;
		$this->load->view($this->viewpath.'/lists', $viewdata);
	}
	
	//广告统计
	function statis()
	{
		if(!$this->is_logined) {
			redirect($this->config->item('admin_path')."/main/login");
		} 
		$manager = $this->mm->get_account();	
		
		//获取列表
		$items = $this->ads_model->list_item(0, u(4), $this->prepage);
		//分页数据处理
		$this->load->library('pagination');
		$config['base_url'] = site_url($this->config->item('admin_path')."/ads/statis/");
		$config['total_rows'] = $this->ads_model->count_items(0);
		$config['per_page'] = $this->prepage;
		$config['uri_segment'] = 4;
		$config['num_links'] = 1;
		$config['first_link'] = '首页';
		$config['last_link'] = '末页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		$this->pagination->initialize($config);
		$pagestring = $this->pagination->create_links();
		
		$viewdata['viewpath'] = $this->viewpath;
		$viewdata['manager'] = $manager;
		$viewdata['items'] = $items;
		$viewdata['pagestring'] = $pagestring;
		$this->load->view($this->viewpath.'/data', $viewdata);
	}
	
	//添加广告位
	function addplace()
	{
		if($dosubmit = p("dosubmit")) {
			$data = array(
				'placename' => p('placename'),
				'width' => p('width'),
				'height' => p('height'),
				'price' => p('price'),
				'passed' => p('passed'),
				'forsite' => p('forsite'),
				'formember' => p('formember'),
			);
			$result = $this->ads_model->addplace($data);
			if($result) {//提示成功
				admin_show_success("添加成功", $this->config->item('admin_path')."/ads/");
			} else {
				admin_show_failed("添加失败");
			}
		}
		$this->load->view($this->viewpath.'/addplace');
	}
	
	//编辑广告位
	function editplace()
	{
		if($dosubmit = p("dosubmit")) {
			$placeid = p("placeid");
			$data = array(
				'placename' => p('placename'),
				'width' => p('width'),
				'height' => p('height'),
				'price' => p('price'),
				'passed' => p('passed'),
				'forsite' => p('forsite'),
				'formember' => p('formember'),
			);
			$result = $this->ads_model->updateplace($placeid, $data);
			if($result) {//提示成功
				admin_show_success("编辑成功", $this->config->item('admin_path')."/ads/");
			} else {
				admin_show_failed("编辑失败");
			}
		}
		$placeid = u(4);
		$viewdata["place"] = $this->ads_model->get_place($placeid);
		$this->load->view($this->viewpath.'/editplace', $viewdata);
	}
	
	//添加广告
	function add()
	{
		if(!strpos($_SERVER["HTTP_USER_AGENT"], "Firefox")) admin_show_failed("该功能只支持火狐浏览器");
		//参数处理
		parse_str($_SERVER["QUERY_STRING"], $this->param);
		$keyword = "";
		if (isset($this->param["keyword"])) {
			$keyword = safeEncoding(trim($this->param["keyword"]));//字符转换处理
		}
		if (isset($this->param["placeid"])) {
			$placeid = $this->param["placeid"];
		}
		$topclassid = $subclassid = 0;
		if (isset($this->param["topclass"])) {
			$topclassid = $this->param["topclass"];
		}
		if (isset($this->param["subclass"])) {
			$subclassid = $this->param["subclass"];
		}
		if(!$placeid) $placeid = 0;
//		$placeid = u(4);
//		if(!$placeid) $placeid = 0;
		if($dosubmit = p("dosubmit")) {
			$data = array(
				'placeid' => p("placeid"),
				'adsname' => p('adsname'),
				'linkurl' => p('linkurl'),
				'fromdate' => strtotime(p('fromdate')),
				'todate' => strtotime(p('todate')),
				'passed' => p('passed'),
				'addtime' => time(),
			);
			$result = $this->ads_model->add($data);
			if($result) {//提示成功
				admin_show_success("添加成功", $this->config->item('admin_path')."/ads/lists");
			} else {
				admin_show_failed("添加失败");
			}
		}
		
		//获取小站列表
//		$sitelists = $this->sm->sitelists();
		$sitelists = $this->sm->listitem2(0, 0, "id", "desc", $keyword, $topclassid, $subclassid);
		//获取分类列表
		$topclass = $subclass = array();
		$topclass = $this->cm->get_subclass();
		if($topclassid>0) {
			$subclass = $this->cm->get_subclass($topclassid);
		}
		//获取广告位列表
		$places = $this->ads_model->list_allplaceitem();
		$current_place = $this->ads_model->get_place($placeid);
		//会员广告位
		if($current_place["formember"] == '1') {
			admin_show_failed("只支持站长广告");
		}
		$viewdata["places"] = $places;
		$viewdata["placeid"] = $placeid;
		$viewdata["current_place"] = $current_place;
		$viewdata["sitelists"] = $sitelists;
		$viewdata['keyword'] = $keyword;
		$viewdata['topclass'] = $topclass;
		$viewdata['subclass'] = $subclass;
		$viewdata['topclassid'] = $topclassid;
		$viewdata['subclassid'] = $subclassid;
		$this->load->view($this->viewpath.'/add', $viewdata);
	}
	
	//编辑广告
	function edit()
	{
		$adsid = u(4);
		if($dosubmit = p("dosubmit")) {
			$adsid = p("adsid");
			$data = array(
				'placeid' => p("placeid"),
				'adsname' => p('adsname'),
				'linkurl' => p('linkurl'),
				'fromdate' => strtotime(p('fromdate')),
				'todate' => strtotime(p('todate')),
				'passed' => p('passed'),
				'addtime' => time(),
			);
			$result = $this->ads_model->update($adsid, $data);
			if($result) {//提示成功
				admin_show_success("编辑成功", $this->config->item('admin_path')."/ads/lists");
			} else {
				admin_show_failed("编辑失败");
			}
		}
		//获取广告
		$row = $this->ads_model->show($adsid);
		//获取广告位列表
		$places = $this->ads_model->list_allplaceitem();
		$current_place = $this->ads_model->get_place($row['placeid']);
		//获取应用小站
		$sites = $this->ads_model->get_adsite($adsid);
		
		$viewdata["places"] = $places;
		$viewdata["current_place"] = $current_place;
		$viewdata["adsid"] = $adsid;
		$viewdata["row"] = $row;
		$viewdata["sites"] = $sites;
		$this->load->view($this->viewpath.'/edit', $viewdata);
	}
	
	//选择删除
	function delselected_place()
	{
		if(!$delstr = u(4)) redirect($this->config->item('admin_path')."/ads/index");
		//删除
		$this->ads_model->delselected_place($delstr);
		admin_show_success("删除成功", $this->config->item('admin_path')."/ads/index");	
	}
	
	//选择屏蔽广告位
	function statuselected_place()
	{
		if(!$delstr = u(4)) redirect($this->config->item('admin_path')."/ads/index");
		//删除
		$this->ads_model->statuselected_place($delstr);
		admin_show_success("屏蔽成功", $this->config->item('admin_path')."/ads/index");	
	}
	
	//选择屏蔽广告
	function statuselected()
	{
		if(!$delstr = u(4)) redirect($this->config->item('admin_path')."/ads/lists");
		//删除
		$this->ads_model->statuselected($delstr);
		admin_show_success("屏蔽成功", $this->config->item('admin_path')."/ads/lists");	
	}
	
	//选择删除广告
	function delselected()
	{
		if(!$delstr = u(4)) redirect($this->config->item('admin_path')."/ads/lists");
		//删除
		$this->ads_model->delselected($delstr);
		admin_show_success("删除成功", $this->config->item('admin_path')."/ads/lists");	
	}
	
	//删除广告
	function delete()
	{
		if(!$adsid = u(4)) redirect($this->config->item('admin_path')."/ads/lists");
		//删除
		$this->ads_model->delete($adsid);
		admin_show_success("删除成功", $this->config->item('admin_path')."/ads/lists");	
	}
	
}

/* End of file */